//加载页面
function addLoadEvent(func){
	var oldonload = window.onload;
	if(typeof window.onload != 'function'){
		window.onload = func;
	}else{
		window.onload = function (){
			oldonload();
			func();
		}
	}
}

function highlightPage(){
	if (!document.getElementsByTagName) return false;

	var links = document.getElementsByTagName("a");
	var linkurl;

	for (var i = 0; i < links.length; i++) {
		linkurl = links[i].getAttribute("href");
		if(window.location.href.indexOf(linkurl) != -1){
			links[i].className = "here";
		}
	}
}
addLoadEvent(highlightPage);
//图片移动处理
function moveElement(elementID,final_x,final_y,interval){
	if(!document.getElementById) return false;
	if(!document.getElementById(elementID)) return false;

	var elem = document.getElementById(elementID);

	if(elem.movement){
		//清除积累上setTimeout队列里的事件
		clearTimeout(elem.movement);
	}

	if(!elem.style.left){
		elem.style.left="0px";
	}
	if(!elem.style.top){
		elem.style.top="0px";
	}

	var xpos = parseInt(elem.style.left);
	var ypos = parseInt(elem.style.top);
	var dist = 0;

	if(xpos==final_x&&ypos==final_y){
		return true;
	}
	if(xpos<final_x){
		dist = Math.ceil((final_x-xpos)/10);
		xpos = xpos+dist;
	}
	if(xpos>final_x){
		dist = Math.ceil((xpos-final_x)/10);
		xpos = xpos-dist;
	}
	if(ypos<final_y){
		dist = Math.ceil((final_y-ypos)/10);
		ypos = ypos+dist;
	}
	if(ypos>final_y){
		dist = Math.ceil((ypos-final_y)/10);
		ypos = ypos-dist;
	}

	elem.style.left = xpos + "px";
	elem.style.top = ypos + "px";
	console.log(xpos);

	var repeat = "moveElement('"+elementID+"','"+final_x+"','"+final_y+"','"+interval+"')";
	//调用setTimeout函数让moveElement函数在预定的时间内发生变化
	elem.movement = setTimeout(repeat,interval);
}

//定义图片移动函数
function prepareSlideshow(){
	//检验DOM
	if(!document.getElementById) return false;
	if(!document.getElementsByTagName) return false;

	//检验元素是否存在
	if(!document.getElementById("preview")) return false;
	// if(!document.getElementById("linklist")) return false;

	//获取这个id
	var preview = document.getElementById("preview");
	var links = document.getElementsByTagName("a");

	//为mouseover事件添加动画效果
	var destination;
	for(var i=0;i<links.length;i++){
		links[i].onmouseover = function(){
			destination = this.getAttribute("href");
			if(destination.indexOf("index.html")!=-1){
				moveElement("preview",0,0,5);
			}
			if(destination.indexOf("about.html")!=-1){
				moveElement("preview",-150,0,5);
			}
			if(destination.indexOf("photos.html")!=-1){
				moveElement("preview",-300,0,5);
			}
			if(destination.indexOf("live.html")!=-1){
				moveElement("preview",-450,0,5);
			}
			if(destination.indexOf("contact.html")!=-1){
				moveElement("preview",-600,0,5);
			}
		}
	}

}
addLoadEvent(prepareSlideshow);

function showWords(id){
	if (!document.getElementsByName) return false;
	if (!document.getElementsByName("section")) return false;

	var sections = document.getElementsByName("section");

	for (var i = 0; i < sections.length; i++) {	
		if(sections[i].id == id){
			sections[i].style.display="block";
		}
		else{
			sections[i].style.display="none";
		}
	}
}

function showPic(whichpic){
	if(!document.getElementById) return false;
	if(!document.getElementById("placeholder")) return false;
	if(!document.getElementById("descrition")) return false;

	// 先获取href的值，存放图片的路径
	var source = whichpic.getAttribute("href");

	// 读取img中的id
	var placeholder = document.getElementById("placeholder");

	//改变图片的src值
	placeholder.setAttribute("src",source);

	//获取title的值
	var text = whichpic.getAttribute("title");

	//读取p中的id
	var descrition = document.getElementById("descrition");

	descrition.firstChild.nodeValue = text;
}

function stripeTable(){
	if (!document.getElementsByTagName) return false;
	// if (!document.getElementsByTagName("table")) return false;
	// if (!document.getElementsByTagName("tr")) return false;

	var tables = document.getElementsByTagName("table");
	var odd,rows;
	for (var i = 0; i < tables.length; i++) {
		odd = false;
		rows = tables[i].getElementsByTagName("tr");
		for (var j = 0; j < rows.length; j++) {
			if(odd == true){
				rows[j].style.backgroundColor = "#ec8";
				odd = false;
			}
			else{
				odd = true;
			}
		}
	}
}
addLoadEvent(stripeTable);

function displayAbbreviations(){
	if (!document.getElementsByTagName || !document.createElement || !document.createTextNode) return false;

	var abbreviations = document.getElementsByTagName("abbr");

	if (abbreviations.length<1) return false;

	//定义存放信息的数组
	var defs = new Array();

	for (var i = 0; i < abbreviations.length; i++) {
		var current_abbr = abbreviations[i];
		var definition = current_abbr.getAttribute("title");
		var key = current_abbr.lastChild.nodeValue;
		defs[key] = definition;
	}

	//定义自定义列表显示
	var dlist = document.createElement("dl");

	//遍历定义
	for( key in defs){
		var definition = defs[key];

		//创建定义标题
		var dtitle = document.createElement("dt");
		var dtitle_text = document.createTextNode(key);
		dtitle.appendChild(dtitle_text);

		//定义描述
		var ddesc = document.createElement("dd");
		var ddesc_text = document.createTextNode(definition);
		ddesc.appendChild(ddesc_text);

		//把他们创建到定义列表
		dlist.appendChild(dtitle);
		dlist.appendChild(ddesc);
	}
	//创建标题
	var header = document.createElement("h1");
	var header_text = document.createTextNode("Abbreviations");
	header.appendChild(header_text);

	var contents = document.getElementsByClassName("content_container");
	if(contents.length == 0) return false;
	var containers = contents[0];
	containers.appendChild(header);
	containers.appendChild(dlist);
}
addLoadEvent(displayAbbreviations);